package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.entity.BabyHeight;
import cn.b.training.baby_education_training.entity.BabyInfo;
import cn.b.training.baby_education_training.repository.BabyHeightRepository;
import cn.b.training.baby_education_training.service.BabyHeightService;
import cn.b.training.baby_education_training.service.BabyInfoService;
import cn.b.training.baby_education_training.service.dto.BabyHeightDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @program: baby_education_training
 * @description
 * @author: wuying
 * @create: 2022-12-01 09:20
 **/
@Service
public class BabyHeightServiceImpl implements BabyHeightService {
    @Autowired
    private BabyHeightRepository babyHeightRepository;
    @Autowired
    private BabyInfoService babyInfoService;
    @Override
    public BabyHeight insertHeight(String recordTime, Float height) throws ParseException {
        BabyHeight babyHeight = new BabyHeight();
        babyHeight.setHeight(height);
        //Calendar calendar = Calendar.getInstance();
        //calendar.setTime(new Date());
        //
        //int year = calendar.get(Calendar.YEAR);
        //int month = calendar.get(Calendar.MONTH)+1;
        //int day = calendar.get(Calendar.DAY_OF_MONTH);
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //String now = year +"-" + month + "-" + day;
        //Date nowTime = sdf.parse(now);
        Date now = new Date();
        Long nowTime = now.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date record = sdf.parse(recordTime);
        Long oldTime = record.getTime();
        long day = (nowTime - oldTime)/(24*60*60*1000);
        long month;
        long year;
        month = day / 30;
        if (month<1){
            babyHeight.setAge(day+"天");
        }else if (month>=1&&month<12){
            babyHeight.setAge(month+"个月");
        }else {
            year = month / 12;
            month = month % 12;
            babyHeight.setAge(year+"年"+month+"个月");
        }
        babyHeight.setRecordTime(record);
        return babyHeight;
    }

    @Override
    public BabyHeight getHeightById(Long id) {
        return babyHeightRepository.findHeightById(id);
    }

    @Override
    public List<BabyHeight> getHeightByBabyId(Long babyId) {
        return babyHeightRepository.findHeightByBabyId(babyId);
    }

    @Override
    public boolean updateHeightInfo(BabyHeightDto heightDto) throws Exception {
        BabyHeight babyHeight = babyHeightRepository.findHeightById(heightDto.getId());
        if (babyHeight==null){
            throw new Exception("未找到身高信息");
        }
        BabyInfo babyInfo = new BabyInfo();
        if (heightDto.getBabyId()!=null){
            babyInfo = babyInfoService.getBabyById(heightDto.getBabyId());
            if (babyInfo==null){
                throw new Exception("未找到婴儿信息");
            }
            babyHeight.setBabyId(heightDto.getBabyId());
        }else {
            babyInfo = babyInfoService.getBabyById(babyHeight.getBabyId());
        }
        babyHeight.setHeight(heightDto.getHeight());
        babyHeight.setAge(heightDto.getAge());
        babyHeight.setRecordTime(Timestamp.valueOf(LocalDateTime.now()));
        babyHeightRepository.save(babyHeight);
        if (babyHeight!=null){
            return true;
        }
        return false;
    }

    @Override
    public Page<BabyHeight> getAllHeights(Integer page, Integer size) {
        return this.babyHeightRepository.findAll(PageRequest.of(page,size));
    }

    @Override
    public boolean deleteHeight(Long id) {
        try{
            this.babyHeightRepository.deleteById(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
